package com.ckzp.jfinal.system.controller;

import com.ckzp.core.json.JsonResult;
import com.ckzp.core.util.DateUtil;
import com.ckzp.jfinal.base.BaseController;
import com.ckzp.jfinal.base.PageSearch;
import com.ckzp.jfinal.form.FormData;
import com.ckzp.jfinal.form.FormSelect;
import com.ckzp.jfinal.interceptor.Admins;
import com.ckzp.jfinal.interceptor.Roles;
import com.ckzp.jfinal.system.WebSite;
import com.ckzp.jfinal.system.cache.SysUserCache;
import com.ckzp.jfinal.system.model.SysWorklog;
import com.ckzp.jfinal.system.model.SysUser;
import com.ckzp.jfinal.system.tools.ValidateRole;
import com.ckzp.plugins.layui.LayuiData;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;
import java.util.List;

/**
 * Created by 51594 on 2020/4/22.
 */
public class SysWorkLogController extends BaseController {


    @Roles(roles = "login")
    public void index() {
        String emp_id = String.valueOf(getAttr("emp_id", ""));
        SysUser user = SysUser.dao.findById(emp_id);
        setAttr("form", FormData.dao().addData("user", user));
        setAttr("R", ValidateRole.n(WebSite.getJwt(getRequest())));
        render("Index.html");
    }


    /**
     * 列表
     */
    @Roles(roles = "system")
    public void SysWorkLogList() {
        FormData fd = FormData.dao();
        setAttr("form", fd);
        render("SysLoginLogList.html");
    }

    /**
     * 操作日志列表
     */
    @Admins(roles = "system", name = "操作日志列表", log = false)
    public void SysOperLogList() {
        FormData fd = FormData.dao();
        setAttr("form", fd);
        render("SysOperLogList.html");
    }


    /**
     * 登陆日志列表
     */
    @Admins(roles = "system", name = "登陆日志列表", log = false)
    public void SysLoginLogList() {
        FormData fd = FormData.dao();
        setAttr("form", fd);
        render("SysLoginLogList.html");
    }

    /**
     * 登陆日志查询API
     */
    @Admins(roles = "system", name = "登陆日志查询API", log = false)
    public void SysOperLogListDataApi() {
        PageSearch ps = PageSearch.n().init(getRequest(), "sys_worklog");
        ps.search("log_type", "<", 98);
        ps.orderby(getRequest(), "log_id desc");
        Page<SysWorklog> pages = ps.paginate(SysWorklog.dao);
        List<SysWorklog> datalist = pages.getList();
        FormSelect fs = FormSelect.n().setSource("`1`成功`2`失败`").setColor("`1`layui-bg-green`2`layui-bg-orange`").setReadOnly(true);
        for (SysWorklog wl : datalist) {
            wl.put("emp_str", SysUserCache.transUserName(wl.mod.getInt("emp_id")));
            wl.put("log_result_str", fs.showText(wl.mod.getInt("log_result")));
            wl.put("log_result_color", wl.mod.getInt("log_result") == 1 ? "layui-bg-green" : "");
        }
        renderJson(LayuiData.newData().setData(datalist).setCount(pages.getTotalRow()));
    }

    /**
     * 登陆日志查询API
     */
    @Admins(roles = "system", name = "登陆日志查询API", log = false)
    public void SysLoginLogListDataApi() {
        PageSearch ps = PageSearch.n().init(getRequest(), "sys_worklog");
        ps.search("log_type", 99);
        ps.orderby(getRequest(), "log_id desc");
        Page<SysWorklog> pages = ps.paginate(SysWorklog.dao);
        List<SysWorklog> datalist = pages.getList();
        FormSelect fs = FormSelect.n().setSource("`1`成功`2`失败`").setColor("`1`layui-bg-green`2`layui-bg-orange`").setReadOnly(true);
        for (SysWorklog wl : datalist) {
            wl.put("emp_str", SysUserCache.transUserName(wl.mod.getInt("emp_id")));
            wl.put("log_result_str", fs.showText(wl.mod.getInt("log_result")));
            wl.put("log_result_color", wl.mod.getInt("log_result") == 1 ? "layui-bg-green" : "");
        }
        renderJson(LayuiData.newData().setData(datalist).setCount(pages.getTotalRow()));
    }


    /**
     * 清空登陆日志Api
     */
    @Admins(roles = "system", name = "清空登陆日志Api", log = false)
    public void EmptyLoginLogApi() {
        int days = getParaInt("days", 365);
        if (days < 1) days = 1;
        Date log_time = DateUtil.addDay(new Date(), (-1 * days));
        Db.delete("delete from sys_worklog where log_type=99 and log_time<?", log_time);
        renderJson(JsonResult.getResult(0, "清除日志成功"));
    }

    /**
     * 清空操作日志Api
     */
    @Admins(roles = "system", name = "清空操作日志Api", log = false)
    public void EmptyOperLogApi() {
        int days = getParaInt("days", 365);
        if (days < 1) days = 1;
        Date log_time = DateUtil.addDay(new Date(), (-1 * days));
        Db.delete("delete from sys_worklog where log_type<99 and log_time<?", log_time);
        renderJson(JsonResult.getResult(0, "清除日志成功"));
    }

    /**
     * 批量删除操作日志Api
     */
    @Admins(roles = "system", name = "批量删除操作日志Api", log = false)
    public void DelMoreOperLogApi() {
        renderJson(DelMoreModel(SysWorklog.dao, getParaStr("keys", ""), "删除日志"));
    }
}